// pages/admin/income/incomeTrend/incomeTrend.js
const app = getApp();
import Api from '../../../../utils/http';
var wxCharts = require('../../../../utils/wxcharts.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pointDetails: null,
    month: [],
    listHis: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      options: options
    });
    that.getLaunchData();
    that.getMonthData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  //周走势图
  canvasWeek:function(){
    var that = this;
    // 历史数据
    let pointDetails = that.data.pointDetails;
    var datas = [];
    console.log(pointDetails)
    for (var i in pointDetails) {
      var obj = {
        data: pointDetails[i].price
      }
      datas = datas.concat(obj.data);
      console.log(datas);
    }
    that.setData({
      datas: datas,
    });
    new wxCharts({
      canvasId: 'lineWeek',
      type: 'line',
      categories: ['第一周', '第二周', '第三周', '第四周','第五周'],
      animation: true,
      background: '#ffffff',
      series: [{
        name: '3月',
        data: datas,
        color: '#00c188',
        format: function (val, name) {
          return val + '元';
        }
      }],
      xAxis: {
        disableGrid: true
      },
      yAxis: {
        // min: 0
      },
      width: 360,
      height: 200,
      dataLabel: true,
      dataPointShape: true,
      legend: false,
      extra: {
        lineStyle: 'curve'
      }
    });
  },
  //cavns数据
  canvasMonth: function () {
    var that = this;
    // 历史数据
    let pointDetails = that.data.pointDetails;
    var month = [];
    var datas = [];
    console.log(pointDetails)
    for (var i in pointDetails) {
      var obj = {
        month: pointDetails[i].month,
        data: pointDetails[i].price
      }
      month = month.concat(obj.month);
      //console.log(month);
      datas = datas.concat(obj.data);
      // console.log(datas);
    }
    that.setData({
      month: month,
      datas: datas,
    });
    // 月走势数据视图
    new wxCharts({
        canvasId: 'lineMonth',
        type: 'line',
        categories: month,
        animation: true,
        background: '#ffffff',
        series: [
          {
            name: '3月',  
            data: datas,
            color: '#00c188',
            format: function (val, name) {
              return val+'元';
            }
          }
        ],
        xAxis: {
          disableGrid: true
        },
        yAxis: {
          // min: 0
        },
        width: 360,
        height: 200,
        dataLabel: true,
        dataPointShape: true,
        legend: false,
        extra: {
          lineStyle: 'curve'
        }
      });
  },
  //投放周走势数据
  getLaunchData: function () {
    var that = this;
    var uid=wx.getStorageSync('userid');
    var param = {
      type: 2,
      selType: 3,
      uid: uid.id,
    };
    console.log(param)
    Api.ajax("/app/Zmanage/launchData", param).then(res => {
      console.log(res)
      if (res.code == 200) {
        that.setData({
          pointDetails: res.data,
        })
        that.canvasWeek();

      }
    });
  },
  //投放月走势数据
  getMonthData: function () {
    var that = this;
    var uid=wx.getStorageSync('userid');
    var param = {
      type: 1,
      selType: 3,
      uid: uid.id,
    };
    console.log(param)
    Api.ajax("/app/Zmanage/launchData", param).then(res => {
      console.log(res)
      if (res.code == 200) {
        that.setData({
          pointDetails: res.data,
        })
        that.canvasMonth();

      }
    });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})